﻿package purplemovies.showreel
{
	import purplemovies.util.VideoQueueLoader;
	import purplemovies.events.VideoEvent;
	
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.events.Event;
	import flash.geom.Point;
	
	import flash.display.Sprite;
	public class ShowReelPlayer extends Sprite{
		
		public var net_connection	:NetConnection;		// Connection to throw video through.
		public var video_container	:Video;				// Video display.
		public var video_loader		:VideoQueueLoader;	// Custom video queue loader.
		public var feature_index	:int;				// Index of current feature being shown.
		//public var paused			:Boolean;			// Weather video is paused.
		
		public function set wind(v:Number){
			if( !v )	resume();	// Wind=0 resume playback.
			else		pause();	// Desired value to move - pause let onFrame handle moving playback.
			_time = _current_stream.time;
			_wind = v;
		}
		public function get wind()	:Number{	return _wind;	}
		private var _wind			:Number;			// The number of seconds desired to wind the video.
		private var _time			:Number;			// Time to set current playback.
		private var _paused			:Boolean;			// Weather video is paused.
		private var _current_stream	:SRStream;			// Video stream currently playing.
		
		private var _features			:Array;				// All features being played/displayed by player.
		public function set features(v){
			_features = v;
			video_loader.queue = v;
		}
		public function get features()		:Array{			return _features;				}
		public function get current_stream():SRStream{		return _current_stream;			}
		public function get steam_time()	:Number{		return _current_stream.time;	}
		
		function ShowReelPlayer(){
			reset();
			
			net_connection		= new NetConnection();
			video_loader		= new VideoQueueLoader(features);
			features			= [];
			video_container		= new Video();
			net_connection.connect(null);
			addChild( video_container );
			
			addEventListener( Event.ENTER_FRAME, onFrame );
			video_loader.addEventListener( VideoEvent.QUEUE_LOADED, onQueueLoaded );
		}
		public function reset(){
			_paused			= false;
			feature_index	= 0;
		}
		
		private function onFrame(e){
			windVideo();	// Do any winding of the video that's requested via the wind property.
		}
		private function windVideo(){
			if( !_wind ) return; // Wind=0 go no furter.
			
			_time += _wind;
			trace(_time, ' ', _current_stream.time);
			if( _time <= 0 ){
				
				pause();					// Pause current steam.
				if( feature_index <= 0  ){	// Feature index is at start of list:
					loadStream( features.length-1 ); // Load up steam at the end of the list.
				}else{
					loadStream( feature_index-1 ); // Load up previous steam.
				}
				seek( _current_stream.video_info.duration );	// Set newly selected current stream time to it's end.
				pause();										// Pause the stream.
				
			}else{
				seek( _time );
			}
		}
		
		public function playItem(p_index:int){
			loadStream(p_index);
			seek(0);
			resume();
		}
		private function loadStream(p_index:int){
			if( p_index >= 0 && p_index < features.length ){ // Requested play index is in range of the video list:
				// Remove any previous end-of-video listener:
				if( _current_stream ) _current_stream.removeEventListener( VideoEvent.END_OF_VIDEO,	onEndOfVideo );
				
				feature_index = p_index; 						// Record for current feature.
				_current_stream = _features[feature_index];		// Set current feature pointer.
				_current_stream.addEventListener( VideoEvent.END_OF_VIDEO,	onEndOfVideo );	// Listen for end of video playback.
				
				if( !_current_stream.video_info ){	// No video meta data yet:
					_current_stream.addEventListener( VideoEvent.META_DATA,	onMetaData );	// Wait for meta data to load.
					
				}else{								// Meta data is already loaded:
					displayVideo();															// Display video.
				}
				
			}
		}
		private function onMetaData(e){
			_current_stream.removeEventListener( VideoEvent.META_DATA,	onMetaData );	// Stop listening for data to come back.
			displayVideo();																// Display video.
			if(!_paused) _current_stream.resume();										// Resume video stream playback.
		}
		private function displayVideo(){	// Setting video display properties and displaying current video stream.
			video_container.width	= _current_stream.dimentions.x; 					// Set video display to streams dimentions.
			video_container.height	= _current_stream.dimentions.y; 
			video_container.attachNetStream( _current_stream );							// Set video displays data source to stream.
		}
		private function onEndOfVideo(e){
			_current_stream.removeEventListener( VideoEvent.END_OF_VIDEO, onEndOfVideo );	// Stop listening for end of this stream.
			trace(e);
			playItem(feature_index+1);
		}
		
		public function seek( p_offset:Number ){
			_current_stream.seek( p_offset );
		}
		public function pause(){
			_paused = true;
			_current_stream.pause();
		}
		public function resume(){
			_paused = false;
			_current_stream.resume();
		}
		
		public function loadVideoQueue(){
			video_loader.load(0);
		}		
		private function onQueueLoaded(e){
			trace(e);
		}
		
	}
}